﻿using System;
using System.Collections.Generic;
using CsvHelper.Configuration;

namespace AIC_Notification
{
    public class Student
    {
        public string Name { get; set; }
        public string Nickname { get; set; }
        public string Program { get; set; }
        public int ApplicationYear { get; set; }
        public string Id { get; set; }
        public string City { get; set; }
        public string PT { get; set; }
        public string ST { get; set; }
        public string EC { get; set; }
        public string KeyStudent { get; set; }
        public string TrelloId { get; set; }
        public bool HasSOFUpdate = false;
        public bool HasGuidanceUpdate = false;

        public List<StudentAdmissionResult> ResultListObject = null;
        public StudentScore ScoreObject = null;

        public Student()
        {
            Name = "";
            Program = "";
            ApplicationYear = -1;
            Id = "";
            City = "";
            PT = "";
            ST = "";
            EC = "";
            TrelloId = "";

            ResultListObject = new List<StudentAdmissionResult>();
        }

        public bool IsEarlyComplete()
        {
            bool IsCompleted = false;

            if (ResultListObject != null)
            {
                foreach (var Result in ResultListObject)
                {
                    if ((Result.ApplicationType == "Early Decision" && Result.ApplicationResult == "Accept") ||
                        (Result.ApplicationType == "Early Action" && Result.ApplicationResult == "Accept" && Result.FinalSelection == true))
                        IsCompleted = true;
                }
            }

            return IsCompleted;
        }

        public StudentAdmissionResult FindAdmissionResult(School ApplyingSchool)
        {
            List<StudentAdmissionResult> SchoolResults = null;

            if (ResultListObject != null)
                SchoolResults = ResultListObject.FindAll(StudentAdmissionResult => StudentAdmissionResult.SchoolID.Equals(ApplyingSchool.ID));

            if (SchoolResults.Count == 0)
                return null;
            else if (SchoolResults.Count == 1)
                return SchoolResults[0];
            else
            {
                Global.SysUtil.Log(String.Format("Student \"{0}\" has duplicate school list: {1}", Name, ApplyingSchool.Name), 2);
                return SchoolResults[0];
            }
        }

        public bool Match(string StudentName, string StudentCity)
        {
            bool NameMatch = false;
            bool CityMatch = false;

            NameMatch = (StudentName.ToLower() == Name.ToLower()) ? true : false;

            CityMatch = (
            ((StudentCity == "BJ" || StudentCity == "Beijing" || StudentCity == "北京") && (City == "Beijing"  || City == "北京")) ||
            ((StudentCity == "CD" || StudentCity == "Chengdu" || StudentCity == "成都") && (City == "Chengdu"  || City == "成都")) ||
            ((StudentCity == "HZ" || StudentCity == "Hangzhou" || StudentCity == "杭州") && (City == "Hangzhou" || City == "杭州")) ||
            ((StudentCity == "SH" || StudentCity == "Shanghai" || StudentCity == "上海") && (City == "Shanghai" || City == "上海")) ||
            ((StudentCity == "SZ" || StudentCity == "Shenzhen" || StudentCity == "深圳") && (City == "Shenzhen" || City == "深圳")) ||
            ((StudentCity == "XA" || StudentCity == "Xian" || StudentCity == "西安") && (City == "Xi'an"    || City == "西安"))
                        ) ? true : false;

            return (NameMatch && CityMatch);
        }

        public string GetCity()
        {
            string StudentCity = "";

            if (City == "BJ" || City == "Beijing" || City == "北京")
                StudentCity = "Beijing";
            else if (City == "CD" || City == "Chengdu" || City == "成都")
                StudentCity = "Chengdu";
            else if (City == "HZ" || City == "Hangzhou" || City == "杭州")
                StudentCity = "Hangzhou";
            else if (City == "SH" || City == "Shanghai" || City == "上海")
                StudentCity = "Shanghai";
            else if (City == "SZ" || City == "Shenzhen" || City == "深圳")
                StudentCity = "Shenzhen";
            else if (City == "XA" || City == "Xian" || City == "Xi'an" || City == "西安")
                StudentCity = "Xian";

            return StudentCity;
        }
    }

    public sealed class StudentMap : ClassMap<Student>
    {
        public StudentMap()
        {
            Map(m => m.Name).Index(0);
            Map(m => m.Nickname).Index(1);
            Map(m => m.City).Index(2);
            Map(m => m.ApplicationYear).Index(3);
            Map(m => m.Id).Index(4);
            Map(m => m.KeyStudent).Index(6);
        }
    }
}
